package com.example.user.myqq;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class DengLu extends AppCompatActivity {
    List<Contentlist> list;
    ListView listView;
    DengLuAdpter dengLuAdpter;
    SmartRefreshLayout refreshLayout;
    int i=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.denglu);
        list = new ArrayList<>();
        listView = (ListView) findViewById(R.id.list_item);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        one();
        //查看详细信息点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                final long SPLASH_LENGTH = 3000;
                Handler handler = new Handler();
                setContentView(R.layout.jiazai);
                handler.postDelayed(new Runnable() {  //使用handler的postDelayed实现延时跳转
                    public void run() {
                        Intent intent = new Intent(DengLu.this,XinXi.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("contentlist",list.get(i));
                        intent.putExtras(bundle);
                        startActivity(intent);
                        finish();
                    }
                }, SPLASH_LENGTH);//3秒后跳转至应用主界面MainActivity

            }
        });


        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //请求网络数据
                OkGo.<String>post("http://route.showapi.com/341-1")
                        .tag(this)
                        .params("showapi_appid","42445")
                        .params("showapi_sign","3b4658317595437795fa6fda62e51245")
                        .params("page",String.valueOf(i))
                        .params("maxResult","50")
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                try {
                                    JSONObject json = new JSONObject(response.body());
                                    JSONObject json2 = json.getJSONObject("showapi_res_body");
                                    JSONArray json3 = json2.getJSONArray("contentlist");
                                    for (int i = 0; i < json3.length(); i++) {
                                        JSONObject j = (JSONObject) json3.get(i);
                                        String title = j.getString("title");
                                        String text = j.getString("text");
                                        int type = j.getInt("type");
                                        String ct = j.getString("ct");
                                        list.add(new Contentlist(title, text, type, ct));
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                //刷新数据
                                dengLuAdpter.notifyDataSetChanged();
                            }
                        });
                i++;
                refreshlayout.finishLoadmore(1000);
            }
        });



        //初始化数据
        dengLuAdpter = new DengLuAdpter(this,list);
        listView.setAdapter(dengLuAdpter);
    }
    public void one(){
        OkGo.<String>post("http://route.showapi.com/341-1")
                .tag(this)
                .params("showapi_appid","42445")
                .params("showapi_sign","3b4658317595437795fa6fda62e51245")
                .params("page",String.valueOf(i))
                .params("maxResult","50")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            JSONObject json = new JSONObject(response.body());
                            JSONObject json2 = json.getJSONObject("showapi_res_body");
                            JSONArray json3 = json2.getJSONArray("contentlist");
                            for (int i = 0; i < json3.length(); i++) {
                                JSONObject j = (JSONObject) json3.get(i);
                                String title = j.getString("title");
                                String text = j.getString("text");
                                int type = j.getInt("type");
                                String ct = j.getString("ct");
                                list.add(new Contentlist(title, text, type, ct));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        //刷新数据
                        dengLuAdpter.notifyDataSetChanged();
                    }
                });
        i++;
    }
}
